#ifndef GDD_INTERFACE_H_
#define GDD_INTERFACE_H_

/***************************************************************************
*===========================================================================
****************************************************************************
* Title        : GDD_Interface.h
*
* Description  : Gesture Detection Driver (GDD) interface header file 
*
* Author  : Misael Munoz
*
****************************************************************************
*===========================================================================
****************************************************************************/


/* System Header Files */

/* Foreign headerfiles */

/* Own headerfiles */


/***************************************************************************
* Definition of exported global data used by Class
* Macros, Enumerations, Types,  Forward declarations
***************************************************************************/
/**
 * Description: Macro to Activate / deactivate debug control 
 */
#define GDD__DEBUG   True


#if GDD__DEBUG == True
/**
 * Description: Adaptation for Debug control, asm instruction to set 
 * a break point. 
 */
#define GDD__DEBUG_BKPT       __asm("bkpt")
#else
/**
 * Description: If Debug switch is not active, do nothing 
 */
#define GDD__DEBUG_BKPT    
#endif 

/**
 * Description: Adaptation name for electrode device variable
 */
#define GDD__u16NewElectrodeStatus        MPR_u16ElectrodeStatus

/**
 * Description: Adaptation name for new data flag profide by the 
 * device driver. 
 */
#define GDD__bNewDataflag              MPR_bNewDataflag


/**
 * Description: Size of the electrode tracker buffer.
 */
#define GDD__nTrackerbuffSize          ((uint8) 10)

/**
 * Description: Number of Electrodes that the module will handle. 
 */
#define GDD__nElectrodes               ((uint8) 12)

/**
 * Description: Number of millisecond to wait for next tap. 
 */
#define GDD__nTapTimer                 ((uint8) 200)

/**
 * Description: Transition from logic One to Logic Zero
 */
#define GDD__nT_OneToCero              ((uint8) 0)

/**
 * Description: Transition from logic Zero to logic One 
 */
#define GDD__nT_CeroToOne              ((uint8) 1)


#define GDD__nRGBnumber                ((uint8) 3)
#define GDD__nRGBRedID                 ((uint8) 2)
#define GDD__nRGBGreenID               ((uint8) 1)
#define GDD__nRGBBlueID                ((uint8) 0)

#define GDD__nIncrement                ((uint8) 1)
#define GDD__nDecrement                ((uint8) 2)

#define GDD__nFixedValue               ((uint8) 150)

/**
 * Description: Adaptation from set PWM value to Red LED
 */
#define GDD__SetPWMRed(_Duty)          set_TPM_CnV(TPM__nTPM1, 1, _Duty);

/**
 * Description: Adaptation from set PWM value to Green LED
 */
#define GDD__SetPWMGreen(_Duty)        set_TPM_CnV(TPM__nTPM1, 0, _Duty);

/**
 * Description: Adaptation from set PWM value to Blue LED
 */
#define GDD__SetPWMBlue(_Duty)         set_TPM_CnV(TPM__nTPM0, 1, _Duty);

/**
 * Description: Enumeration that describes all the states 
 * for each electrode. 
 */
typedef enum {
   enCleared,
   enActive,
   enTap,
   enActive2,
   enTap2,
   enActive3,
   enTap3,
   enTrigger1,
   enTrigger2,
   GDD__MaxStates   
}GDD__tenStates;

/**
 * Description: Typedef struct for each electrode.  
 */
typedef struct{
   GDD__tenStates CurrState;    
}GDD__tstElectrodeData;


   
/***************************************************************************
* CLASS DEFINITION
***************************************************************************/
#ifdef REVERSE_ENGINEERING /* open C++ class */
#define GDD_Interface_C

/**
 * Class for GDD module interface 
 * 
 */
class GDD_CInterface {
public: 
 
#else /* open the instance struct */
typedef struct{
#endif
/***************************************************************************
*   Instance Attributes
***************************************************************************/
   
   /**
   * buffer of structures of electrode information.
   */
   GDD__tstElectrodeData stbElectrodes[GDD__nElectrodes];
   
   /**
   * Electrode status in the near past read.  
   * @values 0x0000 ... 0xFFFF
   */
   uint16 u16PastElectrodeStatus;
   
   /**
   * buffer to store the tracking patter 
   * @values 0x00 ... 0xFF
   */
   uint8 u8bTrackerbuff[GDD__nTrackerbuffSize];
   
   /**
   * Pointer to indicate in which position store. 
   * @values 0x00 ... 0xFF
   */
   uint8 u8Trackerpointer;
   
   /**
   * Pointer to indicate which electrode has been taped
   * @values 0x00 ... 0xFF
   */
   uint8 u8TapTappointer;
   
   /**
   * Software timer ID 
   * @values 0x00 ... 0xFF
   */
   uint8 u8SwTimer;


   
#ifndef REVERSE_ENGINEERING /* close the struct */
}GDD__tstNiData;
#endif

/***************************************************************************
*   Class Attributes
***************************************************************************/

/* System Wide Scope */
#ifdef REVERSE_ENGINEERING
public:
#endif

   
/* File Local Scope */
#ifdef REVERSE_ENGINEERING
private:
#endif

#ifdef GDD_Interface_C
   /**
   * Relative second Counter intended for SwBus. Not resetteable by diagnosis.
   * Used by BC,CBS, etc. Formelly called "Relative Second Timer" (RST)
   * @range  
   * @values 0x000000 ... 0xFFFFFE
   */
   // uint32 GDD__u32RelativeTimer_1s = 0;



#endif

/***************************************************************************
*   Class Operations
***************************************************************************/

/* System Wide Scope */  
#ifdef REVERSE_ENGINEERING
public:
#endif
  
   /**
   * This function is a call-back function called by
   * the EVHD when a new TapTap event is triggered
   * @return void
   * @author Misael Munoz
   * @param  void
   */
   void GDD_vTapTapEventHandler(void);

   /**
   * This function is a call-back function called by
   * the EVHD when a new that is available for 
   * processing.
   * @return void
   * @author Misael Munoz
   * @param  void
   */
   void GDD_vNewDataEventHandler(void);
   
   /**
   * Module initialization function 
   * @return void
   * @author Misael Munoz
   * @param  void
   */
   void GDD_vInit(void);
   
   /**
   * Callback function for handle the timer expired
   * @return void
   * @author Misael Munoz
   * @param  void
   */
   void GDD_vTimerCallback(void);


/* File Local Scope */
#ifdef REVERSE_ENGINEERING
private:
#endif

#ifdef GDD_Interface_C

   /**
   * Local function to clear the tracking buffer.  
   * @return void
   * @author Misael Munoz
   * @param  void
   */
   static void GDD__vClearTrackerBuffer(void);
   
   /**
   * Local function to set to "Clear" the state of 
   * all the electrodes. 
   * @return void
   * @author Misael Munoz
   * @param  void
   */
   static void GDD__vSettoClearState(void);
   
   /**
   * Local function to process Tap state machine. 
   * @return void
   * @author Misael Munoz
   * @param  u16NewStates: 16-bit variable that gives the new states
   */
   static void GDD__vTapStateMachine(uint16 u16NewStates);
   
   /**
   * Local function to process the track pattern direction and 
   * executes the corresponding function for each direction.  
   * @return void
   * @author Misael Munoz
   * @param  u16NewStates: 16-bit variable that gives the new states
   */
   static void GDD__vTrackPattern(uint16 u16NewStates);
   
   static uint8 GDD__u8GetColorPattern(uint16 u16RGBFiltered, uint16 u16NewStates);
   
   /**
   * Local function to get first change on electrodes. 
   * @return uint8: the position of the first changed electrode. 
   * @author Misael Munoz
   * @param  u16States: variable  to get the first state. 
   */
   static uint8 GDD__u8ElectrodeFIFO(uint16 *u16States);
   
#endif


#ifdef REVERSE_ENGINEERING /* close the C++ class */
};
#endif
/********************
**  CLASS END
*********************/

#endif /* GDD_INTERFACE_H_ */
